import axios from "axios";
import zmessage from "@/components/MessageBox/lib";

const SUCCESS = 200;
const NOT_LOGIN = 201;
const service = axios.create({
  // baseURL: process.env.VUE_APP_BASE_HOST,
  baseURL: "/",
  timeout: 10000
});

export function request(options) {
  return new Promise((resolve, reject) => {
    service(options)
      .then(({ data }) => {
        if (data.code === SUCCESS) {
          return resolve(data);
        } else if (data.code === NOT_LOGIN) {
          zmessage.warning("您已被登出，请重新登录");
          reject(data);
        } else {
          // 在出口进行消息提示，而不用去处理每个子接口的消息提示和异常打印
          zmessage.error(data.message);
          reject(data);
        }
      })
      .catch(error => {
        zmessage.error(error.message);
        reject(error);
      });
  });
}
